#!/bin/bash

# 读取配置信息
curl -sSL http://192.168.2.7/my_oschina/fastdfs/env.sh > env.sh
. ./env.sh

# 配置YUM 源
rpm --import http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
rm -f /etc/yum.repos.d/*
curl -so /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -so /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i '/aliyuncs.com/d' /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/epel-7.repo

# 关闭防火墙 selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

# 安装编译及依赖环境
yum install -y gcc gcc-c++ perl libevent

# 编译安装 libfastcommon
cd /opt
curl -SLO ${DOWNURL}/libfastcommon-${LIBFASTCOMMON_VER}.tar.gz --progress
tar xvf libfastcommon-${LIBFASTCOMMON_VER}.tar.gz
cd libfastcommon-${LIBFASTCOMMON_VER}
./make.sh && ./make.sh install
ldconfig -v | grep libfastcommon

# 安装 FastDFS
cd /opt
curl -SLO ${DOWNURL}/fastdfs-${FASTDFS_VER}.tar.gz --progress
tar xvf fastdfs-${FASTDFS_VER}.tar.gz -C $SOFT_DIR
mv ${SOFT_DIR}/fastdfs-${FASTDFS_VER} ${SOFT_DIR}/fastdfs
cd ${SOFT_DIR}/fastdfs
./make.sh && ./make.sh install
cp ${SOFT_DIR}/fastdfs/conf/* /etc/fdfs

# 配置 FastDFS Trackerd
mkdir -p $DATA/trackerd
sed -i 's/^store_group=.*/store_group=group1/' /etc/fdfs/tracker.conf
sed -i "s#^base_path=.*#base_path=$DATA/trackerd#" /etc/fdfs/tracker.conf

# 配置 FastDFS Storage
mkdir -p $DATA/storage
sed -i 's/^tracker_server/# tracker_server/' /etc/fdfs/storage.conf
sed -i "s#^base_path=.*#base_path=$DATA/storage#" /etc/fdfs/storage.conf
sed -i "s#^store_path0=.*#store_path0=$DATA/storage#" /etc/fdfs/storage.conf

# 配置 FastDFS 客户端
mkdir -p $DATA/client
sed -i 's/^tracker_server/# tracker_server/' /etc/fdfs/client.conf
sed -i "s#base_path=.*#base_path=$DATA/client#" /etc/fdfs/client.conf

# 替换配置文件中(storage.conf client.conf)的 TRACKER_SERVER
for server in $TRACKER_SERVER; do
    sed -i "118a $server " /etc/fdfs/storage.conf
    sed -i "14a $server " /etc/fdfs/client.conf
done

# 如果IP不在 TRACKER_SERVER变量里面 使 Tracker 配置文件失效(本机仅做 storage)
if [ ! "$(echo $TRACKER_SERVER | grep `hostname -I`)" ];then
    sed -i 's/disabled=.*/disabled=true/' /etc/fdfs/tracker.conf
    rm -rf $DATA/trackerd
fi

# 编译安装 nginx
cd /opt
yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel
curl -SLO ${DOWNURL}/nginx-${NGINX_VER}.tar.gz --progress
curl -SLO ${DOWNURL}/fastdfs-nginx-module-${FASTDFS_MODULE_VER}.tar.gz --progress
tar xvf nginx-${NGINX_VER}.tar.gz
tar xvf fastdfs-nginx-module-${FASTDFS_MODULE_VER}.tar.gz -C ./nginx-${NGINX_VER}
cd nginx-${NGINX_VER}
export C_INCLUDE_PATH=/usr/include/fastcommon
./configure --prefix=$NGINX_HOME --add-module=./fastdfs-nginx-module-${FASTDFS_MODULE_VER}/src
make && make install
cp fastdfs-nginx-module-${FASTDFS_MODULE_VER}/src/mod_fastdfs.conf /etc/fdfs

# 配置 FastDFS Nginx 模块
sed -i "s#^base_path=.*#base_path=$DATA/storage#" /etc/fdfs/mod_fastdfs.conf
sed -i "s#^store_path0=.*#store_path0=$DATA/storage#" /etc/fdfs/mod_fastdfs.conf
sed -i "s#^url_have_group_name = .*#url_have_group_name = true#" /etc/fdfs/mod_fastdfs.conf
sed -i 's/^tracker_server/# tracker_server/' /etc/fdfs/mod_fastdfs.conf
sed -i "s#^group_count =.*#group_count = 1#" /etc/fdfs/mod_fastdfs.conf
echo -e "\n[group1]\ngroup_name=group1\nstorage_server_port=23000\nstore_path_count=1\nstore_path0=$DATA/storage" >>/etc/fdfs/mod_fastdfs.conf

# 替换配置文件中(mod_fastdfs.conf)的 TRACKER_SERVER
for server in $TRACKER_SERVER; do
    sed -i "40a $server " /etc/fdfs/mod_fastdfs.conf
done


# 配置环境变量
echo "NGINX_HOME=$NGINX_HOME" >> /etc/profile.d/mysoft.sh
echo 'PATH=$NGINX_HOME/sbin:$PATH' >> /etc/profile.d/mysoft.sh

# 配置 nginx
curl -sSL http://192.168.2.7/my_oschina/fastdfs/nginx.conf > $NGINX_HOME/conf/nginx.conf
sed -i "s/8888/$NGINX_PORT/" ${NGINX_HOME}/conf/nginx.conf
echo "<h1>$HOSTNAME $(hostname -I)</h1>" >$NGINX_HOME/html/index.html

# 配置 nginx 代理
mkdir $NGINX_HOME/conf.d
curl -sSL http://192.168.2.7/my_oschina/fastdfs/dfs_proxy.conf >$NGINX_HOME/conf.d/dfs_proxy.conf
sed -i "s/80/$PROXY_PORT/" ${NGINX_HOME}/conf/nginx.conf

# 配置代理的后端服务器
for SERVER in $NGINX_LIST ;do
    sed -i "1a\    server ${SERVER}:${NGINX_PORT} weight=1 max_fails=2 fail_timeout=30s;" ${NGINX_HOME}/conf.d/dfs_proxy.conf
done

# 启动 trackerd 服务
/etc/init.d/fdfs_trackerd start && sleep 3

# 启动 storaged 服务
/etc/init.d/fdfs_storaged restart

# 启动 Nginx
/usr/local/nginx/sbin/nginx

cat <<EOF
    测试  /usr/bin/fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/anti-steal.jpg
    查看状态  /usr/bin/fdfs_monitor /etc/fdfs/client.conf | egrep 'ip_addr|tracker'
EOF
